﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using log4net;
using TanWeb.Helpers;
using TanWeb.Models.Entities;
using TanWeb.Controllers.Business;
using Newtonsoft.Json;

namespace TanWeb.Controllers
{
    public class DetailController : Controller
    {
        //===============================================================
        //  プライベート変数
        //===============================================================
        #region /***    Private Variable Declared    ***/
        /// <summary>
        /// Log4net 変数
        /// </summary>
        protected static readonly ILog logger = LogManager.GetLogger(typeof(Controller));
        #endregion

        [HttpPost]
        [RequiresRouteValues("groupId, subGroupId")]
        public ActionResult Detail(string groupId, string subGroupId)
        {
            logger.Info("Start Controller: Detail - Action: Detail");
            string json = string.Empty;

            DetailBusiness business = new DetailBusiness();

            try
            {
                List<MenuItem> menuItems = business.GetMenuItems();
                List<ProductProfile> lstProductProfile = business.GetProductInfo(groupId, subGroupId, 1);
                int pageCount = business.GetAllPageRecord(groupId, subGroupId);
                string[] nameInfo = business.GetNameInfo(groupId, subGroupId);
                Products products = new Products(pageCount, lstProductProfile, 1, groupId, nameInfo[0], subGroupId, nameInfo[1]);
                Session[Constants.SS_LISTMENUITEM] = menuItems;

                logger.Info("End Controller: Detail - Action: Detail");
                return View(Constants.PAGE_DETAIL_DETAIL, products);
            }
            catch (Exception ex)
            {
                logger.Error("Error Controller: Detail - Action: Detail; ", ex);
                return RedirectToAction("Index", "Home");
            }
        }

        /*
        [RequiresRouteValues("pageIndex, groupId, subGroupId")]
        public string Paginate(int pageIndex, string groupId, string subGroupId)
        {
            logger.Info("Start Controller: Detail - Action: Paginate");
            string json = string.Empty;

            DetailBusiness business = new DetailBusiness();

            try
            {
                List<MenuItem> menuItems = business.GetMenuItems();
                List<ProductProfile> lstProductProfile = business.GetProductInfo(groupId, subGroupId, pageIndex);
                int pageCount = business.GetAllPageRecord(groupId, subGroupId);
                string[] nameInfo = business.GetNameInfo(groupId, subGroupId);
                Products products = new Products(pageCount, lstProductProfile, pageIndex, nameInfo[0], nameInfo[1]);
                Session[Constants.SS_LISTMENUITEM] = menuItems;

                logger.Info("End Controller: Detail - Action: Paginate");
                return JsonConvert.SerializeObject(products);
            }
            catch (Exception ex)
            {
                logger.Error("Error Controller: Detail - Action: Paginate; ", ex);
                return Constants.ERROR_PAGE;
            }
        }
         * */

        [RequiresRouteValues("pageIndex, groupId, subGroupId")]
        public string Paginate(int pageIndex, string groupId, string subGroupId)
        {
            logger.Info("Start Controller: Detail - Action: Paginate");
            string json = string.Empty;

            DetailBusiness business = new DetailBusiness();

            try
            {
                List<MenuItem> menuItems = business.GetMenuItems();
                List<ProductProfile> lstProductProfile = business.GetProductInfo(groupId, subGroupId, pageIndex);

                foreach (ProductProfile product in lstProductProfile)
                {
                    product.ImageName = product.GetTempImagePath();
                }

                int pageCount = business.GetAllPageRecord(groupId, subGroupId);
                string[] nameInfo = business.GetNameInfo(groupId, subGroupId);
                Products products = new Products(pageCount, lstProductProfile, pageIndex, nameInfo[0], nameInfo[1]);
                Session[Constants.SS_LISTMENUITEM] = menuItems;

                logger.Info("End Controller: Detail - Action: Paginate");
                return JsonConvert.SerializeObject(products);
            }
            catch (Exception ex)
            {
                logger.Error("Error Controller: Detail - Action: Paginate; ", ex);
                return Constants.ERROR_PAGE;
            }
        }

        [RequiresRouteValues("groupId, subGroupId, productId")]
        public ActionResult Info(string groupId, string subGroupId, string productId)
        {
            logger.Info("Start Controller: Detail - Action: Info");

            DetailBusiness business = new DetailBusiness();

            try
            {
                int updateRes = business.UpdateViewCount(groupId, subGroupId, productId);
                Product productInfo = business.GetProductInfo(groupId, subGroupId, productId);

                if (updateRes == Constants.ERROR_SQL_CONDITION || productInfo == null)
                {
                    return View("Error");
                }

                logger.Info("End Controller: Detail - Action: Info");
                return View(Constants.PAGE_DETAIL_INFO, productInfo);
            }
            catch (Exception ex)
            {
                logger.Error("Error Controller: Detail - Action: Info", ex);
                return RedirectToAction("Index", "Home");
            }
        }
    }
}
